package com.dms.modules.order.utils;

import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.atomic.AtomicLong;

@Component
public class OrderNoGenerator {
    private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
    private static final AtomicLong COUNTER = new AtomicLong(0);
    private static final int MAX_COUNTER = 9999;

    public String generate() {
        String timestamp = LocalDateTime.now().format(FORMATTER);
        long counter = COUNTER.incrementAndGet() % (MAX_COUNTER + 1);
        if (counter == 0) {
            COUNTER.set(0);
        }
        return String.format("O%s%04d", timestamp, counter);
    }
} 